package cn.edu.cjw.APMFP.Util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.ResourceBundle;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Product.pojo.Product;
import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;
import cn.edu.cjw.APMFP.Resource.Resource;
import cn.edu.cjw.APMFP.RoleRight.pojo.RoleRight;
import cn.edu.cjw.APMFP.WorkStep.pojo.WorkStep;
import cn.edu.cjw.APMFP.user.pojo.User;

public class HBaseUtill {

	private HBaseUtill() {

	}

	/*
	 * 构建hbase连接对象
	 */
	public static Connection getConn() throws IOException {

		ResourceBundle bundle = ResourceBundle.getBundle("System");

		String name = bundle.getString("hbase_connect_name");
		String value = bundle.getString("hbase_connect_value");

		Configuration create = HBaseConfiguration.create();
		create.set(name, value);

		return ConnectionFactory.createConnection(create);

	}

	/*
	 * 构造要插入的数据作为一个put类型（一个put对象只能对应一个rowkey）的对象
	 */
	public static Put putRole(RoleRight r) {

		Put put = new Put(Bytes.toBytes(r.getRNum()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("rname"), Bytes.toBytes(r.getRName()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("rdescribe"), Bytes.toBytes(r.getRDescribe()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("rright"), Bytes.toBytes(r.getRRight().toString()));

		return put;

	}

	/*
	 * 构造要删除的数据作为一个delete类型的对象
	 */
	public static Delete deleteRole(RoleRight r) {

		return new Delete(Bytes.toBytes(r.getRNum()));

	}

	/*
	 * 查找HBase中RowKey为rnum的role数据，并实例化RoleRight对象返回
	 */
	public static RoleRight resultForRole(String rnum, Result result) throws Exception {

		RoleRight roleRight = new RoleRight();

		roleRight.setRNum(rnum);

		// 从结果中取指定key的value
		byte[] nameValue = result.getValue("base_info".getBytes(), "rname".getBytes());
		if (nameValue != null) {
			roleRight.setRName(new String(nameValue));
		} else {
			return roleRight;
		}

		byte[] describeV = result.getValue("base_info".getBytes(), "rdescribe".getBytes());
		if (describeV != null) {
			roleRight.setRDescribe(new String(describeV));
		} else {
			return roleRight;
		}

		byte[] rightV = result.getValue("base_info".getBytes(), "rright".getBytes());
		if (rightV != null) {
			String[] split = new String(rightV).split(",");
			ArrayList<String> arrayList = new ArrayList<String>();
			for (String string : split) {
				arrayList.add(string);
			}
			roleRight.setRRight(arrayList);
		} else {
			return roleRight;
		}

		return roleRight;
	}

	public static Put putUser(User user) {

		Put put = new Put(Bytes.toBytes(user.getAccount()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("account"), Bytes.toBytes(user.getAccount()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("password"), Bytes.toBytes(user.getPassWord()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes(user.getName()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("uRole"), Bytes.toBytes(user.getuRole().getRNum()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes(String.valueOf(user.getAge())));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("Deactivate"),
				Bytes.toBytes(String.valueOf(user.isDeactivate())));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("gender"), Bytes.toBytes(user.getGender()));

		return put;

	}

	public static Put putWStep(WorkStep ws) {

		Put put = new Put(Bytes.toBytes(ws.getWId()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("wname"), Bytes.toBytes(ws.getWName()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("wdescribe"), Bytes.toBytes(ws.getWDescribe()));

		return put;
	}

	public static Put putBOM(BOMTable bomTable) {
		Put put = new Put(Bytes.toBytes(bomTable.getBId()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("BName"), Bytes.toBytes(bomTable.getBName()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("Btype"), Bytes.toBytes(bomTable.getBtype()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("unit"), Bytes.toBytes(bomTable.getUnit()));

		return put;
	}

	public static Put PutProduct(Product product) {
		Put put = new Put(Bytes.toBytes(product.getBId()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("BName"), Bytes.toBytes(product.getBName()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("Btype"), Bytes.toBytes(product.getBtype()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("unit"), Bytes.toBytes(product.getUnit()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("material"),
				Bytes.toBytes(product.getMaterial().toString()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("workstep"),
				Bytes.toBytes(product.getWorkstep().toString()));
		return put;
	}

	public static Put putTaskNotExamine(ProductionTask task) {
		Put put = new Put(Bytes.toBytes(task.getTaskId()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("founder"), Bytes.toBytes(task.getFounder()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("creationTime"),
				Bytes.toBytes(task.getCreationTime().toString()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("chargeMan"), Bytes.toBytes(task.getChargeMan()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("completionTime"),
				Bytes.toBytes(task.getCompletionTime().toString()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("examine"), Bytes.toBytes(String.valueOf(task.getExamine())));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("taskdescribe"), Bytes.toBytes(task.getDescribe()));
		return put;
	}

	public static Put putTaskExamine(ProductionTask task) {
		Put put = new Put(Bytes.toBytes(task.getTaskId()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("founder"), Bytes.toBytes(task.getFounder()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("creationTime"),
				Bytes.toBytes(task.getCreationTime().toString()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("chargeMan"), Bytes.toBytes(task.getChargeMan()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("completionTime"),
				Bytes.toBytes(task.getCompletionTime().toString()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("examine"), Bytes.toBytes(String.valueOf(task.getExamine())));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("percentage"),
				Bytes.toBytes(task.getPercentage().toString()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("product"),
				Bytes.toBytes(task.getProduct().toString()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("material"),
				Bytes.toBytes(task.getMaterial().toString()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("taskdescribe"), Bytes.toBytes(task.getDescribe()));
		return put;
	}

	public static Put putResource(Resource resource) {
		Put put = new Put(Bytes.toBytes(resource.getBId()));

		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("BName"), Bytes.toBytes(resource.getBName()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("Btype"), Bytes.toBytes(resource.getBtype()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("unit"), Bytes.toBytes(resource.getUnit()));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("housenum"),Bytes.toBytes(String.valueOf(resource.getWareHouseNum())));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("ChargeMan"),Bytes.toBytes(resource.getChargeMan()));
		return put;
	}
	/**
	 * 传入构建的table对象和Connection对象，进行关闭
	 */
	public static void Close(Table table, Connection connection) {

		if (table != null) {
			try {
				table.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (connection != null) {
			try {
				connection.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}


}
